package com.example.project.service.impl;

import cn.hutool.db.sql.ConditionGroup;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.project.entity.Depart;
import com.example.project.entity.ProjUser;
import com.example.project.entity.User;
import com.example.project.enums.RoleEnum;
import com.example.project.mapper.DepartMapper;
import com.example.project.mapper.ProjUserMapper;
import com.example.project.mapper.UserMapper;
import com.example.project.service.UserService;
import com.example.project.utils.SecurityUtils;
import com.example.project.vo.model.PageDto;
import com.example.project.vo.model.ProUserVo;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (User)表服务实现类
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Resource
    private DepartMapper departMapper;
    @Resource
    private UserMapper userMapper;

    @Resource
    private ProjUserMapper projUserMapper;

    @Override
    public User queryById(Long id) {
        User user = this.getBaseMapper().selectById(id);
        dealUser(user);
        return user;
    }

    @Override
    public PageInfo<User> queryAll(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<User> list = this.getBaseMapper().selectList(pageDto.toQueryWrapper());
        list.forEach(user -> {
            dealUser(user);
        });
        return new PageInfo<>(list);
    }

    @Override
    public User insert(User user) {
        this.getBaseMapper().insert(user);
        return user;
    }

    @Override
    public User update(User user) {
        this.getBaseMapper().updateById(user);
        return this.queryById(user.getId());
    }

    @Override
    public List<User> querryList() {
        return this.getBaseMapper().selectList(null);
    }

    @Override
    public boolean deleteById(Long id) {
        return this.getBaseMapper().deleteById(id) > 0;
    }


    private void dealUser(User user) {
        Long depId = user.getDepId();
        if (ObjectUtils.isNotEmpty(depId)) {
            user.setDepartName(departMapper.selectOne(new LambdaQueryWrapper<Depart>().eq(Depart::getId, depId)).getName());
        }
    }

    @Override
    public User getMyself() {
        Long userId = SecurityUtils.getUser().getId();
        User user = userMapper.selectById(userId);
        return user;
    }

    @Override
    public List<User> getTree() {
        List<User> leaderList = this.getBaseMapper().selectList(
                new LambdaQueryWrapper<User>()
                        .isNotNull(User::getDepId)
                        .eq(User::getRole, RoleEnum.CUSTOMER)
        );
        leaderList.forEach(user -> {
            dealUser(user);
        });

        return leaderList;
    }


    @Override
    public void proUser(ProUserVo proUserVo) {
        List<Long> userIds = proUserVo.getUserIds();
        Long projId = proUserVo.getProjId();
        userIds =  userIds.stream().distinct().collect(Collectors.toList()); ;
        projUserMapper.delete(new LambdaQueryWrapper<ProjUser>().eq(ProjUser::getProjId,projId));
        userIds.stream().forEach(userId->{
            projUserMapper.insert(new ProjUser(null,projId,userId));
        });
    }

    @Override
    public List<User> proUserList(Long id) {
        List<Long> collect = projUserMapper.selectList(new LambdaQueryWrapper<ProjUser>().eq(ProjUser::getProjId, id)).stream().map(ProjUser::getUserId).collect(Collectors.toList());
        List<User> list =  new ArrayList<User>();
        if(CollectionUtils.isNotEmpty(collect)){
            list= this.getBaseMapper().selectList(new LambdaQueryWrapper<User>().in(User::getId,collect));
        }
        return list;
    }
}
